package root.bignerdranch.wlyy;

import androidx.appcompat.app.AppCompatActivity;

import android.bignerdranch.wlyy.R;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import butterknife.BindView;


public class MainActivity extends AppCompatActivity {
    @BindView(R.id.main_content)
    TextView main_content;
    Handler handler;
    Button btn_url,btn_http,btn_webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*ButterKnife.bind(this);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                if(msg.what==0x1234){
                    main_content.setText(main_content.getText().toString()+msg.getData().getString("content"));
                }
            }
        };
        new Thread(new UdpServer(handler)).start();*/
        btn_url = findViewById(R.id.btn_url);
        btn_http = findViewById(R.id.btn_http);
        btn_webview = findViewById(R.id.btn_webView);
        btn_url.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, URLActivity.class);
                startActivity(intent);
            }
        });
        btn_http.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,HTTP.class);
                startActivity(intent);
            }
        });
        btn_webview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,WEBVIEW.class);
                startActivity(intent);
            }
        });
    }

}

class UdpServer implements Runnable{
    private final static int localPort = 5000;
    byte[] buf = new byte[1024];
    Handler handler;
    DatagramSocket localDs = null;

    public UdpServer(Handler handler){
        this.handler = handler;
        try {
            localDs=new DatagramSocket(localPort);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run(){
        while (true){
            try {
                DatagramPacket localDp= new DatagramPacket(buf,1024);
                localDs.receive(localDp);
                String rec = new String(localDp.getData(),localDp.getOffset(),localDp.getLength());
                Message msg=new Message();
                msg.what=0x1234;
                Bundle bundle=new Bundle();
                bundle.putString("content",rec);
                msg.setData(bundle);
                handler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


